﻿package velbus.modules 
{
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import mx.containers.Panel;
	import mx.controls.TextArea;
	import velbus.Packet;
	import velbus.Velbus;
	import velbus.VelbusEvent;
	
	/**
	* Base class for al modules (like dimmer, relay, ...)
	* @author Gert Stalpaert (gert@stogo.net)
	*/
	public class VelbusModule extends Panel
	{

		public static const RECEIVE_MODULE_NAME_PART_1:int = 0xF0;
		public static const RECEIVE_MODULE_NAME_PART_2:int = 0xF1;
		public static const RECEIVE_MODULE_NAME_PART_3:int = 0xF2;
		public static const RECEIVE_MODULE_STATUS:int = 0xED;

		public static const REQUEST_MODULE_TYPE:int = 0xFF;
		public static const REQUEST_MODULE_STATUS:int = 0xFA;
		public static const REQUEST_MODULE_NAME:int = 0xEF;
		

		protected var _velbus:Velbus;
		protected var _address:int;
		protected var _nameList: /*String*/ Array = new Array();
		protected var _taInfo:TextArea;
		
		public function VelbusModule(velbus:velbus.Velbus, address_:int) 
		{
			//trace( "VelbusModule : " + VelbusModule );
			_velbus = velbus;
			_address = address_;
			title = "Velbus Module";
			width = 250;
			height = 150;
			
			<mx:Tile x="0" y="0"  id="moduleTile" width="100%" height="100%">
					
				</mx:Tile>
			
			_taInfo = new TextArea();
			_taInfo.percentWidth = 100;
			_taInfo.percentHeight = 100;
			this.addChild(_taInfo);
			_velbus.addEventListener(VelbusEvent.PACKET_RECEIVED, onPacketReceivedForName);
			requestNames();
		}
		
		protected function get NrOfNames():int
		{
			return 1;
		}
		
			
		protected function onPacketReceivedForName(e:VelbusEvent):void 
		{
			//return if event is for other address
			if (e.packet.address != _address) {
				return
			}
			
			if (e.packet.command >= RECEIVE_MODULE_NAME_PART_1 && e.packet.command <= RECEIVE_MODULE_NAME_PART_3) {
				setName(e.packet);
			} 
				
		}
		
		
		public static function ArrayToByteArray(a:Array):ByteArray {
			var ba:ByteArray = new ByteArray();
			ba.length = a.length;
			for (var i:int = 0; i < a.length; i++) 
			{
				ba[i] = a[i];
			}
			return ba;
		}
		
		private function requestNames():void {
			var bitPos:int = Math.pow(2, NrOfNames)-1;
			var cmd:Packet = new Packet(_address, Packet.PRIORITY_LOW, 2, ArrayToByteArray([VelbusModule.REQUEST_MODULE_NAME, bitPos]), false);
			Velbus.addToSendQueue(cmd);
		}
		
		
		protected function setName(p:Packet):void
		{
			var bitPos:int = 0;
			var n:int = p.getDataByte(1); //bit
			while (n > 1) {
				bitPos++;
				n = n / 2;
			}
			var pos:int = (p.command - RECEIVE_MODULE_NAME_PART_1) * 6; ///0,6 or 12
			if (!_nameList[bitPos]) {
				_nameList[bitPos] = "                  ";
			}
			
			var nameAr:/*String*/Array = _nameList[bitPos].split("");
			var test:String = "";
			for (var i:int = 2; i <= 8; i++) 
			{
				nameAr[i - 2 + pos] = String.fromCharCode(p.getDataByte(i));
				test += nameAr[i - 2 + pos];
			}
			//trace("setname " + Packet.dec2hex(p.address) + " - bitpos: " + bitPos + " - pos: " + pos + " : " + test + " " + p)
			_nameList[bitPos] = nameAr.join("");
			
			
		
			//remove all after EOL (|)
			var len:int = _nameList[bitPos].indexOf(String.fromCharCode(0xFF));
			if (len > -1) {
				_nameList[bitPos] = _nameList[bitPos].substr(0, len);
			}
			
			//trace(this + " " + bitPos + ": " + _nameList[bitPos])
			_taInfo.text = _nameList.toString();
			dispatchEvent(new VelbusModuleEvent(VelbusModuleEvent.NAME_LIST_CHANGE, p));
			
		}
		
	}
	
}